//
//  RegisterViewController.m
//  EasyToo
//
//  Created by hydb on 13-6-11.
//
//

#import "RegisterViewController.h"
#import "RegisterMobileViewController.h"
#import "AccountManager.h"
#import <QuartzCore/QuartzCore.h>

@interface RegisterViewController ()

@end

@implementation RegisterViewController
{
    UIAlertView *_baseAlert;
    
    UIImageView *_imgview;
    UITextField *_mobileTF;
    UILabel *_descLbl;
    UITextField *_passwordTF;
    UIButton *_genpasBtn;
    UIButton *_registerBtn;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    //初始化视图
    [self init_view];
        
}

- (void)dealloc
{
    [_imgview release];
    [_mobileTF release];
    [_descLbl release];
    [_passwordTF release];
    [_registerBtn release];
    [_genpasBtn release];
    [_registerBtn release];
    
    _imgview = nil;
    _mobileTF = nil;
    _descLbl = nil;
    _passwordTF = nil;
    _genpasBtn = nil;
    _registerBtn = nil;
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





//初始化视图
- (void)init_view
{
    //定义title
    self.title = @"登录/注册";
    //加载背影
    [self.view setBackgroundColor:BackgroundColor];

    
    //头像显示
    _imgview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.png"]];
    _imgview.frame = CGRectMake(120, ADJUST_IOS7STATUES_HIGHT+80, 80, 80);
    [self.view addSubview:_imgview];
    
    //用户名
    _mobileTF = [[UITextField alloc] initWithFrame:CGRectMake(20, ADJUST_IOS7STATUES_HIGHT+170, 280, 50)];
    _mobileTF.font = [UIFont fontWithName:@"Arial" size:18.0f];
    _mobileTF.textColor = [UIColor blackColor];
    _mobileTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _mobileTF.borderStyle = UITextBorderStyleRoundedRect;
    _mobileTF.delegate = self ;
    _mobileTF.returnKeyType =UIReturnKeyDone;
    _mobileTF.layer.borderColor = [UIColor grayColor].CGColor;
    _mobileTF.layer.borderWidth = 1 ;
    _mobileTF.layer.cornerRadius = 10 ;
    _mobileTF.keyboardType = UIKeyboardTypeNumberPad;
    _mobileTF.placeholder = [NSString stringWithFormat:@"账户名"];
    _mobileTF.text = self.mobile;
    _mobileTF.clearsOnBeginEditing = YES;
    [self.view addSubview:_mobileTF];
    
    //说明
    _descLbl = [[UILabel alloc]initWithFrame:CGRectMake(20, ADJUST_IOS7STATUES_HIGHT+220, 280, 20)];
    _descLbl.text = @"请确认手机号码无误，注册成功后将无法修改";
    _descLbl.backgroundColor = [UIColor clearColor];
    _descLbl.font = [UIFont systemFontOfSize:20];
    _descLbl.adjustsFontSizeToFitWidth = YES;
    _descLbl.textColor = [UIColor grayColor];
    if (self.type == 1) {
        [self.view addSubview:_descLbl];
    }
    
    //密码
    _passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(20, ADJUST_IOS7STATUES_HIGHT+250, 100, 40)];
    _passwordTF.font = [UIFont fontWithName:@"Arial" size:18.0f];
    _passwordTF.textColor = [UIColor blackColor];
    _passwordTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _passwordTF.borderStyle = UITextBorderStyleRoundedRect;
    _passwordTF.delegate = self ;
    _passwordTF.returnKeyType =UIReturnKeyDone;
    _passwordTF.layer.borderColor = [UIColor grayColor].CGColor;
    _passwordTF.layer.borderWidth = 1 ;
    _passwordTF.layer.cornerRadius = 10 ;
    _passwordTF.text = @"";
    _passwordTF.placeholder = [NSString stringWithFormat:@"验证码"];
    _passwordTF.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:_passwordTF];
    
    //生成手机验证码
    _genpasBtn = [[UIButton alloc] initWithFrame:CGRectMake(200, ADJUST_IOS7STATUES_HIGHT+250, 100, 40)];
    NSString *gencode = [NSString stringWithFormat:@"%d%d%d%d%d%d", arc4random() % 10, arc4random() % 10, arc4random() % 10, arc4random() % 10, arc4random() % 10, arc4random() % 10];
    [_genpasBtn setTitle:gencode forState:UIControlStateNormal];
    [_genpasBtn setBackgroundColor:[UIColor grayColor]];
    [_genpasBtn setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
    _genpasBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _genpasBtn.layer.borderColor = [UIColor grayColor].CGColor;
    _genpasBtn.layer.borderWidth = 1 ;
    _genpasBtn.layer.cornerRadius = 10 ;
    [self.view addSubview:_genpasBtn];
    [_genpasBtn addTarget:self action:@selector(genpassEvent) forControlEvents:UIControlEventTouchUpInside];

    //验证手机验证码
    _registerBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, ADJUST_IOS7STATUES_HIGHT+300, 280, 40)];
    [_registerBtn setTitle:@"下一步" forState:UIControlStateNormal];
    _registerBtn.titleLabel.font = [UIFont systemFontOfSize:20];
    [_registerBtn setBackgroundColor:[UIColor redColor]];
    [_registerBtn setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
    _registerBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _registerBtn.layer.borderColor = [UIColor grayColor].CGColor;
    _registerBtn.layer.borderWidth = 1 ;
    _registerBtn.layer.cornerRadius = 10 ;
    [self.view addSubview:_registerBtn];
    [_registerBtn addTarget:self action:@selector(registerEvent) forControlEvents:UIControlEventTouchUpInside];

}



//重新生成验证码
- (void)genpassEvent
{
    NSString *gencode = [NSString stringWithFormat:@"%d%d%d%d%d%d", arc4random() % 10, arc4random() % 10, arc4random() % 10, arc4random() % 10, arc4random() % 10, arc4random() % 10];
    [_genpasBtn setTitle:gencode forState:UIControlStateNormal];
}

//注册
- (void)registerEvent
{
    //检测用户名密码手机号是否为空
    if ([_mobileTF.text isEqualToString:@""] == YES || [_passwordTF.text isEqualToString:@""] == YES) {
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"注册出错" message:@"手机号、验证码不能为空"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
        [_baseAlert show];
    }
    
    //判断手机号码格式
    else if ([DeviceUtil isMobileNumber:_mobileTF.text] == NO) {
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"注册出错" message:@"手机号格式出错"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
        [_baseAlert show];
    }
    
    //检验验证码
    else if (![_genpasBtn.titleLabel.text isEqualToString:_passwordTF.text]){
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"验证码出错" message:@"请输入正确的验证码"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(performDismiss:) userInfo:nil repeats:NO];
        [_baseAlert show];
    }
    //成功注册
    else{
        RegisterMobileViewController *registerController = [[RegisterMobileViewController alloc] init];
        registerController.mobile = _mobileTF.text;
        registerController.type = self.type;
        [self.navigationController pushViewController:registerController animated:YES];
        [registerController release];
    }
}


- (void) performDismiss: (NSTimer *)timer {
    [_baseAlert dismissWithClickedButtonIndex:0 animated:NO];//important
    [_baseAlert release];
    _baseAlert = NULL;
}


//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
    
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
    if(offset > 0)
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
    
    [UIView commitAnimations];
}


//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}


#pragma mark UITextFieldDelegate
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_mobileTF resignFirstResponder];
    [_passwordTF resignFirstResponder];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


@end
